#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEditor;

public class UIPathSwitchMember : IEditorSpecialMember
{
    public static string uiPath = "Assets/Resources/Prefabs/UI";
    public static Dictionary<GameObject, string> allUIPrefabs;
    public static Dictionary<string, GameObject> allUINamePath;

    public override void DisplayInEditor(object inst, float standardWidth, string label, object jeaVal, MemberInfo member, MemberInfo jeaMember, int index = 0)
    {
        
        if(jeaVal == null || ((List<object>)jeaVal).Count == 0)
        {
            this.SetEditorValue(ref inst, jeaMember, TutorialJsonUtils.GetValueOfMember(inst, member));
        }
        var realJeaVal = (List<object>)jeaVal;
        if (realJeaVal.Count <= index)
        {
            return;
        }
        while (realJeaVal.Count <= index)
        {
            return;
        }
        if (index  == 0)
        {
            ShowParent(inst, standardWidth, label, jeaVal, member, jeaMember, index);
        }
        else
        {
            ShowChild(inst, standardWidth, label, jeaVal, member, jeaMember, index);
        }
    }

    protected void ShowParent(object inst, float standardWidth, string label, object jeaVal, MemberInfo member, MemberInfo jeaMember, int index = 0)
    {
        var realJeaVal = (List<object>)jeaVal;
        var newSelect = EditorGUILayout.ObjectField(label, (GameObject)realJeaVal[index], typeof(GameObject), false);
        var realVal = new List<string>();
        if(newSelect == (GameObject)realJeaVal[index])
        {
            return;
        }
        
        if (newSelect != null && allUIPrefabs.ContainsKey((GameObject)newSelect))
        {
            realVal.Add(allUIPrefabs[(GameObject)newSelect]);
            for (int i = 1; i < realJeaVal.Count; i++)
            {
                realJeaVal[i] = "";
                realVal.Add("");
            }
            realJeaVal[index] = newSelect;
            TutorialJsonUtils.SetValueOfMember(ref inst, member, realVal);
            TutorialJsonUtils.SetValueOfMember(ref inst, jeaMember, realJeaVal);
        }
        else
        {
            realJeaVal = new List<object>() { newSelect };
            realVal = new List<string>() { "" };
            TutorialJsonUtils.SetValueOfMember(ref inst, member, realVal);
        }
    }

    protected void ShowChild(object inst, float standardWidth, string label, object jeaVal, MemberInfo member, MemberInfo jeaMember, int index = 0)
    {
        var realJeaVal = (List<object>)jeaVal; 
        if (realJeaVal[0] == null)
        {
            return;
        }
        while (realJeaVal.Count <= index)
        {
            return;
        }
        if (realJeaVal[index].GetType() != typeof(string))
            realJeaVal[index] = "";
        Transform trans = ((GameObject)realJeaVal[0]).transform;
        List<string> aa = GetChildStringList(trans, "");
        aa.Insert(0, "");

        string oldValue = (string)realJeaVal[index];
        string newValue;

        ///如果老的数据是空串， 那么就不用给出动态路径部分
        if (string.IsNullOrEmpty(oldValue))
        {
            newValue = aa[EditorGUILayout.Popup(label, aa.IndexOf(oldValue), aa.ToArray())];
        }
        else
        {
            string[] retList = oldValue.Split('#');
            if (aa.IndexOf(retList[0]) == -1)
            {
                newValue = "";
            }
            else
            {
                if (retList.Length == 1)
                {
                    retList = new string[2] { retList[0], "" };
                }
                newValue = aa[EditorGUILayout.Popup(label, aa.IndexOf(retList[0]), aa.ToArray())];
                if (!string.IsNullOrEmpty(newValue))
                {
                    string spValue = EditorGUILayout.TextField("动态路径", retList[1]);
                    newValue = string.Format("{0}#{1}", newValue, spValue);
                }
            }
            
        }
      
        if (newValue == oldValue)
        {
            return;
        }
        realJeaVal[index] = newValue;
        var realVal = (List<string>)TutorialJsonUtils.GetValueOfMember(inst, member);
        realVal[index] = (string)realJeaVal[index];
        TutorialJsonUtils.SetValueOfMember(ref inst, member, realVal);
        TutorialJsonUtils.SetValueOfMember(ref inst, jeaMember, realJeaVal);
        

    }

    public List<string> GetChildStringList(Transform trans, string parent)
    {
        List<string> retList = new List<string>();
        for (int i = 0; i < trans.childCount; i++)
        {
            Transform child = trans.GetChild(i);
            string sName = parent + child.name;
            retList.Add(sName);
            retList.AddRange(GetChildStringList(child, sName + "/"));
        }
        return retList;
    }

    public override void SetEditorValue(ref object inst, MemberInfo member, object val)
    {
		List<string> realval = (List<string>)val;
		if (Application.platform == RuntimePlatform.WindowsEditor) {
			for (int i = 0; i < realval.Count; ++i) {
				if (i == 0) {
					realval[i] = realval[i].Replace ("/", "\\\\");
					realval[i] = realval[i].Replace("\\\\", "\\");
				} else
					realval[i] = realval[i].Replace("\\", "/");
			}
		} else {
			for (int i = 0; i < realval.Count; ++i) {
				realval[i] = realval[i].Replace("\\", "/");
			}
		}
        if (allUIPrefabs == null)
        { 
            allUIPrefabs = new Dictionary<GameObject, string>();
            allUINamePath = new Dictionary<string, GameObject>();
            UITools.GetAllObjsInDir(uiPath, LayerMask.NameToLayer("UI"), allUIPrefabs, allUINamePath);
        }
        List<object> jreval = new List<object>();
        if (allUINamePath.ContainsKey(realval[0]))
        {
            jreval.Add(allUINamePath[realval[0]]);
            for (int i = 1; i < realval.Count; i++)
            {
                jreval.Add(realval[i]); 
            }
        }
        else
        {
            jreval.Add(null);
            jreval.Add("");
        }

        TutorialJsonUtils.SetValueOfMember(ref inst, member, jreval);
    }
}
	#endif

